package org.geektimes.util;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ExecutorsSubmitDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Result r = new Result();
        r.setA("111");
        final ExecutorService executorService = Executors.newFixedThreadPool(2);
        //result这个对象是主线程和子线程之间的桥梁
        //<T> Future<T> submit(Runnable task, T result);
        final Future<Result> future = executorService.submit(new Task(r), r);
        final Result result = future.get();
        System.out.println(r == result);
        System.out.println(result.getA());
        System.out.println(result.getX());
        System.out.println(r.getA() == result.getA());
        System.out.println(r.getX() == result.getX());
    }
}

class Result{
    String a;
    String x;

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public String getX() {
        return x;
    }

    public void setX(String x) {
        this.x = x;
    }
}

class Task implements Runnable{
    Result r;
    Task(Result r){
        this.r = r;
    }

    @Override
    public void run() {
        System.out.println(r.getA());
        r.setX("222");
    }
}
